<template>
    <div class="set-containter">
        <el-tabs type="border-card" v-if="loadFlag">
            <el-tab-pane label="1商家信息设置">
				<el-merchant 
				@submitMerchant="setMerInfo" 
				:merchant-info="merchantInfo" ref="mer" @setMerchantService="setMerchantService"></el-merchant>
			</el-tab-pane>
            <el-tab-pane label="2服务配置" :disabled="c_filter_info">
				<el-ser-config @subSerInfo="subSerInfo" :merchant-info="merchantInfo" ref="ser"  :cart-id-list="carts"></el-ser-config>
			</el-tab-pane>
            <el-tab-pane label="3小程序配置" :disabled="c_filter_info">
				<el-wx-config @subWxInfo="subWxInfo" :merchant-info="merchantInfo" ref="wer"></el-wx-config>
			</el-tab-pane>
			<el-row class="box-flex">
				<el-button type="primary" class="pri-btn" @click="save">保存</el-button>
			</el-row>
        </el-tabs>
		
    </div>
</template>

<script>
	import elMerchant from './child/elMerchant.vue'
	import elSerConfig from './child/elSerConfig.vue'
	import elWxConfig from './child/elWxConfig.vue'
	import {
		SUCCESS_REQUEST_STATUS
	} from '@/utils/config.js'
	export default {
		props: {
			// 商家ID
			merchantId: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				ser: true,
				wxInfo: null,
				merInfo: null,
				serInfo: null,
				loadFlag: false,
				catIds: [],
				carts: null,
				c_filter_info: true,
				merchantInfo: {},
				
			}
		},
		created() {
			if(this.merchantId) {
				this._initMerchantInfo()
				return
			}
			this.loadFlag = true
		},
		methods: {
			setMerchantService(val) {
				this.c_filter_info = val
			},
			_initMerchantInfo() {
				
				this.$ajax({
					api: 'merchant/info',
					params: {
						merchantId: this.merchantId
					}
				}).then(res => {
					console.log(res)
					if(res.status === SUCCESS_REQUEST_STATUS) {
						console.log(res.data.catIds)
						this.carts = res.data.catIds
						this.merchantInfo = Object.assign({}, res.data.merchant, res.data.wechatMerchant)
						console.log(this.merchantInfo)
						this.loadFlag = true
					}
				})
			},
			// 服务信息
			subSerInfo(info ,carts) {
				this.serInfo = info
				this.carts = carts
				
			},
			// 微信信息
			subWxInfo(info) {
				this.wxInfo = info
			},
			save(info) {
				this.$refs.mer.submitMerchant()
				this.$refs.ser.subSerInfo()
				this.$refs.wer.subWxInfo()
				if(this.merInfo){
					this.saveInfo()
				}
			},
			setMerInfo(merInfo) {
				this.merInfo = merInfo
			},
			saveInfo() {
				var params = new URLSearchParams()
				let par = {
					merchantCode: this.merInfo.merchantCode,
					loginName: this.merInfo.loginName,
					merchantType: this.merInfo.merchantType,
					merchantName: this.merInfo.merchantName,
					companyName: this.merInfo.merchantName,
					companyPhone: this.merInfo.companyPhone,
					address: this.merInfo.address,
					contactName: this.merInfo.contactName,
					contactMobile: this.merInfo.contactMobile,
					contract: this.merInfo.contract,
					remark: this.merInfo.remark,
					style: this.serInfo ? this.serInfo.style :'simple',
					miniAppsEntrance: this.serInfo ? this.serInfo.miniAppsEntrance : false,
					h5Entrance: this.serInfo ? this.serInfo.h5Entrance : false,
					basicMall: this.serInfo ? this.serInfo.basicMall : false,
					seniorMall: this.serInfo ? this.serInfo.seniorMall : false,
					componentSearch: this.serInfo ? this.serInfo.componentSearch : false,
					showModelRela: this.serInfo ? this.serInfo.showModelRela : false,
					memberAudit: this.serInfo ? this.serInfo.memberAudit : false,
					mustLogin: this.serInfo ? this.serInfo.mustLogin : false,
					vinSearch: this.serInfo ? this.serInfo.vinSearch : false,
					vinNum: this.serInfo ? this.serInfo.vinNum : 0,
					maintenanceUpload: this.serInfo ? this.serInfo.maintenanceUpload: false,
					endTime: this.serInfo ? this.serInfo.endTime : '',
					pubAppId: this.wxInfo ? this.wxInfo.pubAppId : '',
					pubAppSecret: this.wxInfo ? this.wxInfo.pubAppSecret : '',
					appId: this.wxInfo ? this.wxInfo.appId : '',
					secret: this.wxInfo ? this.wxInfo.secret : '',
					wechatMerchantCode: this.wxInfo ? this.wxInfo.wechatMerchantCode : '',
					apiKey: this.wxInfo ? this.wxInfo.apiKey : '',
					apiKey: this.wxInfo ? this.wxInfo.apiKey : '',
					merchantId: this.merchantId ? this.merchantId : ''
				}
				for(var key in par){
					params.append(key,par[key]);
				}
				this.carts.forEach((item) => {
					 params.append('catIds',item);
				})
				this.$http.post('http://116.62.22.58:18897/merchant/save', params).then((res) => {
					console.log(res)
					if(res.status === SUCCESS_REQUEST_STATUS) {
						this.$message({
							message:'操作成功',
							type:'success'
						})
						this.$emit('saveMerchantInfo')
					}
				}).catch(err => {
					console.log(err)
				})
			},
			
		},
		components: {
			elMerchant,
			elSerConfig,
			elWxConfig
		}
	}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
    .set-containter {
        display: flex;
        flex: 1;
		height: 100%;
		box-sizing: border-box;
        flex-direction: column;
		.box-flex{
			display: flex;
			align-items: center;
			justify-content: center;
			text-align: center;
		}
    }
</style>
